{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pymannkendall as mk\n",
    "import geopandas as gpd\n",
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_parquet(\"outputs/d2-events-2d5-230929.parquet\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>GEOID</th>\n",
       "      <th>wfday</th>\n",
       "      <th>heatday</th>\n",
       "      <th>smoke_pm_non_zero</th>\n",
       "      <th>smoke_pm_gt_five</th>\n",
       "      <th>hw</th>\n",
       "      <th>_hws</th>\n",
       "      <th>hs</th>\n",
       "      <th>hws</th>\n",
       "      <th>ws</th>\n",
       "      <th>...</th>\n",
       "      <th>wfday_2d</th>\n",
       "      <th>_hws_2d</th>\n",
       "      <th>hs_2d</th>\n",
       "      <th>hws_2d</th>\n",
       "      <th>ws_2d</th>\n",
       "      <th>hw_2d</th>\n",
       "      <th>smoke_pm_gt_five_2d</th>\n",
       "      <th>hs_2d_5</th>\n",
       "      <th>ws_2d_5</th>\n",
       "      <th>hws_2d_5</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>time</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2006-01-01</th>\n",
       "      <td>04001942600</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>False</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006-01-01</th>\n",
       "      <td>04001942700</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>False</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006-01-01</th>\n",
       "      <td>04001944000</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>False</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006-01-01</th>\n",
       "      <td>04001944100</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>False</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006-01-01</th>\n",
       "      <td>04001944201</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>False</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 25 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                  GEOID  wfday  heatday  smoke_pm_non_zero  smoke_pm_gt_five  \\\n",
       "time                                                                           \n",
       "2006-01-01  04001942600  False    False              False             False   \n",
       "2006-01-01  04001942700  False    False              False             False   \n",
       "2006-01-01  04001944000  False    False              False             False   \n",
       "2006-01-01  04001944100  False    False              False             False   \n",
       "2006-01-01  04001944201  False    False              False             False   \n",
       "\n",
       "               hw   _hws     hs    hws     ws  ...  wfday_2d  _hws_2d  hs_2d  \\\n",
       "time                                           ...                             \n",
       "2006-01-01  False  False  False  False  False  ...         0    False      0   \n",
       "2006-01-01  False  False  False  False  False  ...         0    False      0   \n",
       "2006-01-01  False  False  False  False  False  ...         0    False      0   \n",
       "2006-01-01  False  False  False  False  False  ...         0    False      0   \n",
       "2006-01-01  False  False  False  False  False  ...         0    False      0   \n",
       "\n",
       "            hws_2d  ws_2d  hw_2d  smoke_pm_gt_five_2d  hs_2d_5  ws_2d_5  \\\n",
       "time                                                                      \n",
       "2006-01-01       0      0      0                    0        0        0   \n",
       "2006-01-01       0      0      0                    0        0        0   \n",
       "2006-01-01       0      0      0                    0        0        0   \n",
       "2006-01-01       0      0      0                    0        0        0   \n",
       "2006-01-01       0      0      0                    0        0        0   \n",
       "\n",
       "            hws_2d_5  \n",
       "time                  \n",
       "2006-01-01         0  \n",
       "2006-01-01         0  \n",
       "2006-01-01         0  \n",
       "2006-01-01         0  \n",
       "2006-01-01         0  \n",
       "\n",
       "[5 rows x 25 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = df.reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['year'] = df['time'].dt.year"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "trends_df = df.groupby(['year'])[[\n",
    "    \"ws\",\n",
    "    \"hw\",\n",
    "    \"hs\",\n",
    "    \"hws\",\n",
    "    \"_hws\",\n",
    "    \"wfday\",\n",
    "    \"hs5\",\n",
    "    \"hws5\",\n",
    "    \"ws5\",\n",
    "    \"heatday\",\n",
    "    \"smoke_pm_non_zero\",\n",
    "    \"smoke_pm_gt_five\",\n",
    "]].sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ws</th>\n",
       "      <th>hw</th>\n",
       "      <th>hs</th>\n",
       "      <th>hws</th>\n",
       "      <th>_hws</th>\n",
       "      <th>wfday</th>\n",
       "      <th>hs5</th>\n",
       "      <th>hws5</th>\n",
       "      <th>ws5</th>\n",
       "      <th>heatday</th>\n",
       "      <th>smoke_pm_non_zero</th>\n",
       "      <th>smoke_pm_gt_five</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>year</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2006</th>\n",
       "      <td>3063</td>\n",
       "      <td>230</td>\n",
       "      <td>12113</td>\n",
       "      <td>121</td>\n",
       "      <td>439100</td>\n",
       "      <td>8910</td>\n",
       "      <td>2542</td>\n",
       "      <td>29</td>\n",
       "      <td>1081</td>\n",
       "      <td>114346</td>\n",
       "      <td>331129</td>\n",
       "      <td>88733</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2007</th>\n",
       "      <td>3271</td>\n",
       "      <td>288</td>\n",
       "      <td>20717</td>\n",
       "      <td>207</td>\n",
       "      <td>426738</td>\n",
       "      <td>9343</td>\n",
       "      <td>8313</td>\n",
       "      <td>134</td>\n",
       "      <td>1434</td>\n",
       "      <td>90041</td>\n",
       "      <td>351423</td>\n",
       "      <td>118034</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008</th>\n",
       "      <td>2962</td>\n",
       "      <td>151</td>\n",
       "      <td>28276</td>\n",
       "      <td>105</td>\n",
       "      <td>609846</td>\n",
       "      <td>8712</td>\n",
       "      <td>13247</td>\n",
       "      <td>66</td>\n",
       "      <td>1289</td>\n",
       "      <td>84559</td>\n",
       "      <td>547859</td>\n",
       "      <td>214184</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2009</th>\n",
       "      <td>1885</td>\n",
       "      <td>92</td>\n",
       "      <td>20078</td>\n",
       "      <td>59</td>\n",
       "      <td>383271</td>\n",
       "      <td>7083</td>\n",
       "      <td>8079</td>\n",
       "      <td>27</td>\n",
       "      <td>373</td>\n",
       "      <td>83340</td>\n",
       "      <td>314844</td>\n",
       "      <td>85975</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2010</th>\n",
       "      <td>1598</td>\n",
       "      <td>92</td>\n",
       "      <td>9395</td>\n",
       "      <td>46</td>\n",
       "      <td>278274</td>\n",
       "      <td>5846</td>\n",
       "      <td>416</td>\n",
       "      <td>2</td>\n",
       "      <td>224</td>\n",
       "      <td>113277</td>\n",
       "      <td>170190</td>\n",
       "      <td>21122</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        ws   hw     hs  hws    _hws  wfday    hs5  hws5   ws5  heatday  \\\n",
       "year                                                                     \n",
       "2006  3063  230  12113  121  439100   8910   2542    29  1081   114346   \n",
       "2007  3271  288  20717  207  426738   9343   8313   134  1434    90041   \n",
       "2008  2962  151  28276  105  609846   8712  13247    66  1289    84559   \n",
       "2009  1885   92  20078   59  383271   7083   8079    27   373    83340   \n",
       "2010  1598   92   9395   46  278274   5846    416     2   224   113277   \n",
       "\n",
       "      smoke_pm_non_zero  smoke_pm_gt_five  \n",
       "year                                       \n",
       "2006             331129             88733  \n",
       "2007             351423            118034  \n",
       "2008             547859            214184  \n",
       "2009             314844             85975  \n",
       "2010             170190             21122  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trends_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ws\n",
      "Mann_Kendall_Test(trend='no trend', h=False, p=0.37305311625145254, z=0.8907689867497083, Tau=0.18095238095238095, s=19.0, var_s=408.3333333333333, slope=72.42857142857143, intercept=2220.0)\n",
      "\n",
      "hw\n",
      "Mann_Kendall_Test(trend='no trend', h=False, p=0.585735213583062, z=0.545026613863917, Tau=0.11428571428571428, s=12.0, var_s=407.3333333333333, slope=5.2, intercept=134.6)\n",
      "\n",
      "hs\n",
      "Mann_Kendall_Test(trend='increasing', h=True, p=0.02944810421427424, z=2.1774353009437313, Tau=0.42857142857142855, s=45.0, var_s=408.3333333333333, slope=4297.5, intercept=-1806.5)\n",
      "\n",
      "hws\n",
      "Mann_Kendall_Test(trend='no trend', h=False, p=0.45734964084351426, z=0.7432181098144324, Tau=0.1523809523809524, s=16.0, var_s=407.3333333333333, slope=6.0, intercept=79.0)\n",
      "\n",
      "_hws\n",
      "Mann_Kendall_Test(trend='increasing', h=True, p=0.017530703373053846, z=2.3753839646658887, Tau=0.4666666666666667, s=49.0, var_s=408.3333333333333, slope=34352.166666666664, intercept=313382.8333333334)\n",
      "\n",
      "wfday\n",
      "Mann_Kendall_Test(trend='no trend', h=False, p=1.0, z=0.0, Tau=0.009523809523809525, s=1.0, var_s=408.3333333333333, slope=2.857142857142857, intercept=7324.0)\n",
      "\n",
      "hs5\n",
      "Mann_Kendall_Test(trend='no trend', h=False, p=0.13764584551826475, z=1.4846149779161806, Tau=0.29523809523809524, s=31.0, var_s=408.3333333333333, slope=1237.0, intercept=-346.0)\n",
      "\n",
      "hws5\n",
      "Mann_Kendall_Test(trend='no trend', h=False, p=0.37305311625145254, z=0.8907689867497083, Tau=0.18095238095238095, s=19.0, var_s=408.3333333333333, slope=4.4, intercept=35.199999999999996)\n",
      "\n",
      "ws5\n",
      "Mann_Kendall_Test(trend='no trend', h=False, p=0.37305311625145254, z=0.8907689867497083, Tau=0.18095238095238095, s=19.0, var_s=408.3333333333333, slope=96.45454545454545, intercept=405.81818181818187)\n",
      "\n",
      "heatday\n",
      "Mann_Kendall_Test(trend='increasing', h=True, p=0.017530703373053846, z=2.3753839646658887, Tau=0.4666666666666667, s=49.0, var_s=408.3333333333333, slope=7365.222222222223, intercept=65060.44444444444)\n",
      "\n",
      "smoke_pm_non_zero\n",
      "Mann_Kendall_Test(trend='increasing', h=True, p=0.02944810421427424, z=2.1774353009437313, Tau=0.42857142857142855, s=45.0, var_s=408.3333333333333, slope=32652.5, intercept=215514.5)\n",
      "\n",
      "smoke_pm_gt_five\n",
      "Mann_Kendall_Test(trend='no trend', h=False, p=0.4884223166225936, z=0.6928203230275509, Tau=0.14285714285714285, s=15.0, var_s=408.3333333333333, slope=6872.333333333333, intercept=58576.66666666667)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for col in trends_df.columns:\n",
    "    print(col)\n",
    "    result = mk.original_test(trends_df[col])\n",
    "    print(result)\n",
    "    print(\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
